#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>

#define MAXN 110
#define MAXM 1010

int tim[MAXN];	int val[MAXN];
int dp[MAXM][MAXM];

using namespace std;

int main(){
	int t;	int m;
	cin >> t >> m;
	for(int i = 1; i <= m; i++){
		scanf("%d %d", &tim[i], &val[i]);
	}
	for(int i = 1; i <= m; i++){
		for(int j = t; j >= 1; j--){
			if(j >= tim[i])
				dp[i][j] = max ( dp[i - 1][j - tim[i]] + val[i], dp[i - 1][j] );
			else
				dp[i][j] = dp[i - 1][j];
		}
	}
	cout << dp[m][t] << endl;	
	return 0;
}